package cloud.tianai.ram.policy.client.aop;

import cloud.tianai.ram.policy.client.annotation.Action;
import org.aopalliance.aop.Advice;
import org.springframework.aop.Pointcut;
import org.springframework.aop.support.AbstractPointcutAdvisor;
import org.springframework.aop.support.annotation.AnnotationMatchingPointcut;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.core.Ordered;

/**
 * @Author: 天爱有情
 * @date 2022/3/24 10:41
 * @Description @Action 注解 增强
 */
public class ActionAnnotationAdvisor extends AbstractPointcutAdvisor implements BeanFactoryAware, Ordered {


    private Advice advice;
    private Pointcut pointcut;

    public ActionAnnotationAdvisor(Advice advice) {
        this.advice = advice;
        this.pointcut = buildPointcut();
    }

    @Override
    public Pointcut getPointcut() {
        return pointcut;
    }

    @Override
    public Advice getAdvice() {
        return advice;
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        if (this.advice instanceof BeanFactoryAware) {
            ((BeanFactoryAware) this.advice).setBeanFactory(beanFactory);
        }
    }


    private Pointcut buildPointcut() {
        return AnnotationMatchingPointcut.forMethodAnnotation(Action.class);
    }
}
